﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using RealtorsPortal.Entities;

namespace RealtorsPortal.Data_Access_Layer
{
    public class ParkagesDAL
    {
        DataAccessHelper daAccHelp = new DataAccessHelper();

        public DataTable getPackagesData()
        {
            DataTable dt = new DataTable();
            dt = daAccHelp.GetDataTable("sp_Get_Parkages");
            return dt;
        }

        
        public DataTable GetPackageByName(Package packObj)
        {
            DataTable dt = new DataTable();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            
            param = new SqlParameter("@PackageName", SqlDbType.VarChar);
            param.Value = packObj.PackageName;
            lstParams.Add(param);
            dt = daAccHelp.GetData("sp_Get_Package_By_Name", lstParams);
            return dt;
        }

        public DataTable GetPackageById(Package packObj)
        {
            DataTable dt = new DataTable();

            List<SqlParameter> paraList = new List<SqlParameter>();
            SqlParameter para;

            para = new SqlParameter("@PackageId", SqlDbType.NVarChar);
            para.Value = packObj.PackageId;
            paraList.Add(para);

            dt = daAccHelp.GetData("sp_Get_Packages_By_Id", paraList);
            return dt;
        }
        /// <summary>
        /// insert Package into database using procedure
        /// </summary>
        /// <param name="pkg">Package object to insert</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool InsertPackage(Package pkg)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each Package properties and add to params list
            ///
            ///
            ///
            param = new SqlParameter("@PackageName", SqlDbType.VarChar);
            param.Value = pkg.PackageName;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Price", SqlDbType.Money);
            param.Value = pkg.Price;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NumOfAds", SqlDbType.Int);
            param.Value = pkg.NumOfAds;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NumOfImg", SqlDbType.Int);
            param.Value = pkg.NumOfImg;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Duration", SqlDbType.Int);
            param.Value = pkg.Duration;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@IsActive", SqlDbType.Bit);
            param.Value = pkg.IsActive;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_Insert_Package", lstParams);

            //return value the indipkg that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }

        /// <summary>
        /// Update Package from DB method
        /// </summary>
        /// <param name="pkg">new Package to update</param>
        /// <param name="oldPackageId">the ID of the Package which we want to change</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool UpdatePackage(Package pkg, int oldPackageId)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each Package properties and add to params list
            ///
            ///
            /// 

            param = new SqlParameter("@PackageId", SqlDbType.Int);
            param.Value = oldPackageId;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@PackageName", SqlDbType.VarChar);
            param.Value = pkg.PackageName;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Price", SqlDbType.Money);
            param.Value = pkg.Price;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NumOfAds", SqlDbType.Int);
            param.Value = pkg.NumOfAds;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@NumOfImg", SqlDbType.Int);
            param.Value = pkg.NumOfImg;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Duration", SqlDbType.Int);
            param.Value = pkg.Duration;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@IsActive", SqlDbType.Bit);
            param.Value = pkg.IsActive;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_Update_Package", lstParams);

            //return value the indipkg that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }


        /// <summary>
        /// Delete Package from DB method
        /// </summary>
        /// <param name="PackageId"> the pkg id to delete</param>
        /// <returns>true if success, otherwise return false</returns>

        public bool DeletePackage(int PackageId)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            param = new SqlParameter("@PackageId", SqlDbType.Int);
            param.Value = PackageId;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_Delete_Package", lstParams);

            //return value the indipkg that the Deleting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }
    }
}